/*
 * YAJHFC - Yet another Java Hylafax client
 * Copyright (C) 2005-2011 Jonas Wolz <info@yajhfc.de>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  
 *  Linking YajHFC statically or dynamically with other modules is making 
 *  a combined work based on YajHFC. Thus, the terms and conditions of 
 *  the GNU General Public License cover the whole combination.
 *  In addition, as a special exception, the copyright holders of YajHFC 
 *  give you permission to combine YajHFC with modules that are loaded using
 *  the YajHFC plugin interface as long as such plugins do not attempt to
 *  change the application's name (for example they may not change the main window title bar 
 *  and may not replace or change the About dialog).
 *  You may copy and distribute such a system following the terms of the
 *  GNU GPL for YajHFC and the licenses of the other code concerned,
 *  provided that you include the source code of that other code when 
 *  and as the GNU GPL requires distribution of source code.
 *  
 *  Note that people who make modified versions of YajHFC are not obligated to grant 
 *  this special exception for their modified versions; it is their choice whether to do so.
 *  The GNU General Public License gives permission to release a modified 
 *  version without this exception; this exception also makes it possible 
 *  to release a modified version which carries forward this exception.
 */
package yajhfc.filters;

import java.text.FieldPosition;
import java.text.Format;

/**
 * @author jonas
 *
 */
public class ConcatStringFilter<V extends FilterableObject, K extends FilterKey> extends AbstractStringFilter<V, K> {

    protected Object[] concatVals;
    protected Object[] resolvedConcatVals;
    protected Class<K> keyClass;
    
    protected StringBuffer concatBuffer = new StringBuffer();
    private FieldPosition dummyFieldPos;
    protected FieldPosition getDummyFieldPos() {
        if (dummyFieldPos == null) {
            dummyFieldPos = new FieldPosition(0);
        }
        return dummyFieldPos;
    }
    
    /**
     * Creates a new ConcatStringFilter. When a match is performed the objects specified
     * in concatVals are concatenated first as follows:
     * If an element is instanceof keyClass, it is treated as column and the respective value is fetched,
     * else simply the element's toString() method is called
     * @param concatVals
     * @param operator
     * @param compareValue
     * @param caseSensitive
     */
    public ConcatStringFilter(Class<K> keyClass, Object[] concatVals, StringFilterOperator operator,
            String compareValue, boolean caseSensitive) {
        super(operator, compareValue, caseSensitive);
        this.concatVals = concatVals;
        this.keyClass = keyClass;
    }

    @SuppressWarnings("unchecked")
    public void initFilter(FilterKeyList<K> columns) {
        resolvedConcatVals = new Object[concatVals.length];
        for (int i=0; i < concatVals.length; i++) {
            Object val = concatVals[i];
            if (keyClass.isInstance(val)) {
                resolvedConcatVals[i] = columns.translateKey((K)val);
            } else {
                resolvedConcatVals[i] = null;
            }
        }
    }

    public boolean matchesFilter(V filterObj) {
        concatBuffer.setLength(0);
        for (int i=0; i < resolvedConcatVals.length; i++) {
            Object key = concatVals[i];
            Object resolvedKey = resolvedConcatVals[i];
            if (resolvedKey == null) {
                concatBuffer.append(key);
            } else {
                Object v = filterObj.getFilterData(resolvedKey);
                if (v != null) {
                    Format colFormat = ((FilterKey)key).getFormat();
                    if (colFormat == null) {
                        concatBuffer.append(v);
                    } else {
                        colFormat.format(v, concatBuffer, getDummyFieldPos());
                    }
                } // else append nothing...
            }
        }
        return doActualMatch(concatBuffer.toString());
    }

    @SuppressWarnings("unchecked")
    public boolean validate(FilterKeyList<K> columns) {
        for (int i=0; i < concatVals.length; i++) {
            Object val = concatVals[i];
            if (keyClass.isInstance(val)) {
                if (!columns.containsKey((K)val)) {
                    return false;
                }
            }
        }
        return true;
    }

    public Object[] getConcatVals() {
        return concatVals;
    }
    
    @Override
    protected void fieldToString(StringBuilder appendTo) {
        appendTo.append('[');
        for (int i=0; i < concatVals.length; i++) {
            Object val = concatVals[i];
            if (keyClass.isInstance(val)) {
                appendTo.append(val);
            } else {
                appendTo.append('\"').append(val).append('\"');
            }
            if (i < concatVals.length - 1) {
                appendTo.append('+');
            }
        }
        appendTo.append(']');
    }
}
